<template>
  <div>
    <!-- <p>文件预览</p> -->
    <ImgPreview />
    <VideoPreview />
    <FilePreview />
  </div>
</template>
<script>
import { FILETYPE } from '@/utils/config'
import Bus from '@/utils/bus.js'
export default {
  components: {
    ImgPreview: () => import('@/components/resource/imgPreview.vue'),
    VideoPreview: () => import('@/components/resource/videoPreview.vue'),
    FilePreview: () => import('@/components/resource/filePreview.vue'),
  },
  mounted() {
    Bus.$on('showPreview', (v, fileList) => {
      let fileArr = ''
      if (
        fileList &&
        fileList.length &&
        fileList[0].hasOwnProperty('sysAttachment')
      ) {
        fileArr = fileList.map((v) => v.sysAttachment)
      } else {
        fileArr = fileList
      }
      this.preview(v, fileArr)
    })
  },
  beforeDestroy() {
    Bus.$off('showPreview')
  },
  methods: {
    preview(v, fileList) {
      let row = v.hasOwnProperty('sysAttachment') ? v.sysAttachment : v
      let { attrSuffixStr } = row
      if (FILETYPE.img.includes(attrSuffixStr)) {
        //图片预览
        if (fileList && fileList.length) {
          fileList = fileList.filter((item) =>
            FILETYPE.img.includes(item.attrSuffixStr),
          )
        }
        Bus.$emit('showDialogimgPreview', row, fileList)
      } else if (FILETYPE.video.includes(attrSuffixStr)) {
        //视频预览
        Bus.$emit('showDialogVideoPreview', row)
      } else if (FILETYPE.file.includes(attrSuffixStr)) {
        //文件预览
        Bus.$emit('showDialogFilePreview', row)
      } else {
        this.$message({
          message: '该文件格式不支持预览',
          type: 'warning',
        })
      }
    },
  },
}
</script>